package com.inspinia.config.common.config;

import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.ResourceBundle;

/**
 * redis配置
 * 优先使用配置中心的配置，如果没有配置文件使用本地配置
 * @author Veblen
 */
@Slf4j
@Data
public class RedisConfig {

    /**
     * redis.properties的配置数据
     */
    public static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle("redis");
    public static String getString(String key) {
        return RESOURCE_BUNDLE.getString(key);
    }

    public static String host = getString("redis.host");
    public static int port = Integer.valueOf(getString("redis.port"));
    public static int timeout = Integer.valueOf(getString("redis.timeout"));
    public static String password = getString("redis.password");
    public static int expire = Integer.valueOf(getString("redis.expire"));

    public static boolean state = Boolean.valueOf(getString("redis.state"));
    public static int maxTotal = Integer.valueOf(getString("redis.maxtotal"));
    public static int maxIdle = Integer.valueOf(getString("redis.maxidle"));
    public static int maxWaitmillis = Integer.valueOf(getString("redis.maxwaitmillis"));
    public static boolean testOnBorrow = Boolean.valueOf(getString("redis.testOnBorrow"));

    public static boolean shardedState = Boolean.valueOf(getString("redis.sharded.state"));
    public static int shardedMaxtotal = Integer.valueOf(getString("redis.sharded.maxtotal"));
    public static int shardedMaxidle = Integer.valueOf(getString("redis.sharded.maxidle"));
    public static int shardedMaxwaitmillis = Integer.valueOf(getString("redis.sharded.maxwaitmillis"));


    /**
     *  监听配置
     * @param changeEvent
     */
    @ApolloConfigChangeListener("redis")
    public void redisChange(ConfigChangeEvent changeEvent){
        log.info("Changes for namespace {}", changeEvent.getNamespace());
        for (String key : changeEvent.changedKeys()) {
            ConfigChange change = changeEvent.getChange(key);
            log.info("Change - key: {}, oldValue: {}, newValue: {}, changeType: {}",
                    change.getPropertyName(), change.getOldValue(), change.getNewValue(),
                    change.getChangeType());
            if ("redis.host".equals(change.getPropertyName())) {
                host = change.getNewValue();
            }
            if ("redis.port".equals(change.getPropertyName())) {
                host = change.getNewValue();
            }
            if ("redis.timeout".equals(change.getPropertyName())) {
                host = change.getNewValue();
            }
            if ("redis.password".equals(change.getPropertyName())) {
                host = change.getNewValue();
            }
            if ("redis.expire".equals(change.getPropertyName())) {
                host = change.getNewValue();
            }
            if ("redis.state".equals(change.getPropertyName())) {
                host = change.getNewValue();
            }
            if ("redis.maxtotal".equals(change.getPropertyName())) {
                host = change.getNewValue();
            }
            if ("redis.expire".equals(change.getPropertyName())) {
                host = change.getNewValue();
            }
            if ("redis.expire".equals(change.getPropertyName())) {
                host = change.getNewValue();
            }
            if ("redis.expire".equals(change.getPropertyName())) {
                host = change.getNewValue();
            }
            if ("redis.expire".equals(change.getPropertyName())) {
                host = change.getNewValue();
            }
            if ("redis.expire".equals(change.getPropertyName())) {
                host = change.getNewValue();
            }
            if ("redis.expire".equals(change.getPropertyName())) {
                host = change.getNewValue();
            }
            if ("redis.expire".equals(change.getPropertyName())) {
                host = change.getNewValue();
            }
        }
    }
}
